﻿/*
 * zerrno_unittest.cxx
 *
 *  Created on: 2015年4月10日
 *      Author: Fifi Lyu
 */

#include <gtest/gtest.h>
#include "zpsoe/zfilesys.h"
#include "zpsoe/zexception.h"
#include <cstdlib>

using namespace zpsoe;

void mk_test_dir(const string &path) {
#ifdef PLATFORM_WIN32
    const string cmd_("mkdir " + path + TO_NULL);
    system(cmd_.c_str());
#else
    const string cmd_("mkdir -p " + path + TO_NULL);
    system(cmd_.c_str());
#endif
}

void rm_test_dir(const string &path) {
#ifdef PLATFORM_WIN32
    const string cmd_("rd /S /Q " + path + TO_NULL);
    system(cmd_.c_str());
#else
    const string cmd_("rm -rf " + path + TO_NULL);
    system(cmd_.c_str());
#endif
}

TEST(ZerrnoTest, is_exist) {
    mk_test_dir("test_dir");
    EXPECT_TRUE(zfilesys::is_exist("test_dir"));

    rm_test_dir("test_dir");
    EXPECT_FALSE(zfilesys::is_exist("test_dir"));
}

TEST(ZerrnoTest, create_file_and_delete_file) {
    EXPECT_TRUE(zfilesys::create_file("test_file"));

    EXPECT_TRUE(zfilesys::delete_file("test_file"));
    EXPECT_TRUE(zfilesys::delete_file("test_file123"));
}

TEST(ZerrnoTest, is_file) {
    zfilesys::create_file("test_file");
    zfilesys::mk_dir("test_dir");

    EXPECT_TRUE(zfilesys::is_file("test_file"));
    EXPECT_THROW(zfilesys::is_file("test_file123"), zpsoe::ZpsoeException);
    EXPECT_FALSE(zfilesys::is_file("test_dir"));

    zfilesys::delete_file("test_file");
    zfilesys::rm_dir("test_dir");
}

TEST(ZerrnoTest, is_dir) {
    zfilesys::create_file("test_file");
    zfilesys::mk_dir("test_dir");

    EXPECT_TRUE(zfilesys::is_dir("test_dir"));
    EXPECT_FALSE(zfilesys::is_dir("test_file"));

    zfilesys::delete_file("test_file");
    zfilesys::rm_dir("test_dir");
}

TEST(ZerrnoTest, mk_dir_and_rm_dir) {
    rm_test_dir("test_dir");
    EXPECT_TRUE(zfilesys::mk_dir("test_dir"));
    EXPECT_TRUE(zfilesys::is_exist("test_dir"));

    EXPECT_TRUE(zfilesys::rm_dir("test_dir"));
    EXPECT_FALSE(zfilesys::is_exist("test_dir"));

    rm_test_dir("test_dir");
    const string sub_dir_("test_dir" + OS_SEPARATOR + "sub1" + OS_SEPARATOR + "sub2");
    EXPECT_TRUE(zfilesys::mk_dir(sub_dir_, true));
    EXPECT_TRUE(zfilesys::is_exist(sub_dir_));

    EXPECT_TRUE(zfilesys::rm_dir("test_dir", true));
    EXPECT_FALSE(zfilesys::is_exist(sub_dir_));
}

TEST(ZerrnoTest, read_file1_and_write_file1) {
    string s_("");

    EXPECT_NO_THROW(zfilesys::write_file("test_file", "1234567890"));
    EXPECT_NO_THROW(s_ = zfilesys::read_file("test_file"));
    EXPECT_STREQ("1234567890", s_.c_str());

    zfilesys::delete_file("test_file");
}

TEST(ZerrnoTest, read_file2_and_write_file2) {
    vector<string> v1_;
    v1_.push_back("1234567890");
    v1_.push_back("abcdefg");
    v1_.push_back("ABCDEFG");

    vector<string> v2_;

    EXPECT_NO_THROW(zfilesys::write_file("test_file", v1_));
    EXPECT_NO_THROW(zfilesys::read_file("test_file", v2_));
    EXPECT_EQ(3, v2_.size());
    EXPECT_STREQ("1234567890", v2_[0].c_str());
    EXPECT_STREQ("abcdefg", v2_[1].c_str());
    EXPECT_STREQ("ABCDEFG", v2_[2].c_str());

    zfilesys::delete_file("test_file");
}

TEST(ZerrnoTest, get_size) {
    zfilesys::write_file("test_file", "1234567890");

    const byte_t size_ = zfilesys::get_size("test_file");
    EXPECT_EQ(10, size_);

    zfilesys::delete_file("test_file");
}

TEST(ZerrnoTest, get_files_in_dir) {
    zfilesys::rm_dir("test_dir", true);
    zfilesys::mk_dir("test_dir/sub", true);
    zfilesys::create_file("test_dir/f1.txt");
    zfilesys::create_file("test_dir/f2.txt");

    vector<file_stat_t> v_;

    EXPECT_NO_THROW(zfilesys::get_files_in_dir("test_dir", FT_ALL, v_));
    EXPECT_EQ(3, v_.size());

    EXPECT_NO_THROW(zfilesys::get_files_in_dir("test_dir", FT_FILE, v_));
    EXPECT_EQ(2, v_.size());

    const bool f1_ = (v_[0].name == "f1.txt" || v_[1].name == "f1.txt");
    const bool f2_ = (v_[0].name == "f2.txt" || v_[1].name == "f2.txt");
    EXPECT_TRUE(f1_ && f2_);

    EXPECT_NO_THROW(zfilesys::get_files_in_dir("test_dir", FT_DIR, v_));
    EXPECT_EQ(1, v_.size());
    EXPECT_STREQ("sub", v_[0].name.c_str());

    zfilesys::rm_dir("test_dir", true);
}
